package edu.sctu.practice.class01.zhengsenwen.JavaLesson.guava;

import com.google.common.base.Charsets;
import com.google.common.io.Files;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;

/**
 * Created by zhengsenwen on 2015/11/26.
 */
public class Longzu2 {

    public static void main(String[] args) {

        String url = "http://www.99csw.com/book/3546/index.htm";

        try {
            Document document = (Document) Jsoup.connect(url).get();

            Elements elements = document.select("dd > a");

            for (Element element : elements) {

                String title = element.text();
                String detailUrl = element.attr("href");

                Files.append(element.text() + " - " + element.attr("href"), new File("D:\\龙族2.txt"), Charsets.UTF_8);
                Files.append("\r\n", new File("D:\\龙族2.txt"), Charsets.UTF_8);

                getContent(title, detailUrl);
            }


        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static void getContent(String title, String detailUrl) {

        try {
            Document document = (Document) Jsoup.connect("http://www.99csw.com" + detailUrl).timeout(10000).get();

            Elements element = document.select("div#content");
            String content = element.text();

            System.out.println(content);

            Files.createParentDirs(new File("D:\\龙族2\\demo.txt"));
            Files.write(content.getBytes(),(new File("D:\\龙族2\\" + title + ".txt")));


        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
